<?php
$basedir = dirname(__FILE__);

require_once $basedir . '/../classes/minime.php';
require_once $basedir . '/../extlib/Crypt/RSA.php';
require_once $basedir . '/../extlib/Crypt/Random.php';
require_once $basedir . '/../extlib/Crypt/Hash.php';
require_once $basedir . '/../extlib/Math/BigInteger.php';

MiniMe::initialize();

if( count($argv) != 5) {
      echo "\n\tERROR!";
      echo '\n\t\tPlease provide USERNAME "JID1,JID2,..." "TargetURL" "TargetTitle" as parameters to this function.';
      echo "\n\n\tExample:";
      echo "\n\t\t" . 'php subscribeuser.php koski "tkoski@identi.ca, pamela@playboyt.com" "http://www.flickr.com/photos/walkah/4831708666/sizes/m/in/set-72157624587812344/" "OSFW2010 Summit Attendees."';
      echo "\n\n";
      exit;
}

$tagger         = $argv[1];
$users          = explode(",", $argv[2]);
$targetURL      = $argv[3];
$targetTitle    = $argv[4];

$userLoader = new Minime_User_Loader(Minime::$conf);
$user = $userLoader->getUser($tagger);

if(!$user) {
    echo "\n\t\Tagging user is not local user!";
    echo "\n\n";
    exit;
}

echo "\n\tStarting to tag users:";

foreach ($users as $contact) {
    $contact = trim($contact);
    $discoverer = new Discovery_Uri($contact);
    $discoverer->__debugoutput = true;
    if(!$discoverer->discover()) {
        echo "\n\tDiscovery of '" . $contact . "' Failed!\n\n";
        continue;
    }
    echo "\n\tFound tagged user:";
    echo "\n\t\t- Name   : '" . $discoverer->getName() . "'.";
    echo "\n\t\t- Salmon : '" . $discoverer->getSalmonUri() . "'.";
    $salmon = new Salmon_Tag();
    if($salmon->createAtomEntryAndSendDoEverythingThisIsTemporarTest($user, $discoverer, $targetURL, $targetTitle)) {
        echo "\n\t\t- Successfully posted.";
    } else {
        echo "\n\t\t- Salmon slap failed: " . file_get_contents("/tmp/minime-salmon-result.error");
    }
    
}
